package service

import (
	"context"
	"encoding/json"
	"errors"
	"service/basic/config"
	__ "service/basic/proto"
	"service/handler/model"
	"time"
)

// 隐藏模块
func (s *Server) ModuleRemove(_ context.Context, in *__.ModuleRemoveReq) (*__.ModuleRemoveResp, error) {
	var home model.Home
	tx := config.DB.Begin() //开启事务
	//defer func() {
	//	if err := recover(); err != nil {
	//		tx.Rollback()
	//		panic(err)
	//	}
	//	if tx.Error != nil {
	//		panic(tx.Error)
	//	}
	//}()
	home.Module = in.Module
	err := home.Remove(tx)
	if err != nil {
		tx.Rollback()
		return nil, errors.New("模块隐藏失败")
	}
	if tx.RowsAffected < 1 {
		tx.Rollback()
		panic(err)
	}
	if tx.Error != nil {
		panic(tx.Error)
	}

	//同步redis
	jsonData, _ := json.Marshal(&home)
	err = config.Rdb.Set(config.Ctx, "module"+in.Module, jsonData, time.Hour).Err()
	if err != nil {
		tx.Rollback()
	}
	tx.Commit()
	return &__.ModuleRemoveResp{}, nil
}

// 列表
func (s *Server) Class(_ context.Context, in *__.ClassListReq) (*__.ClassListResp, error) {
	var class model.Class
	classList, err := class.ListPage(in.Page, in.PageSize)
	if err != nil {
		return nil, errors.New("首页列表展示失败")
	}
	var user model.User
	//if err := user.GetUserId(); err != nil {
	//	return nil, errors.New("用户id查找失败")
	//}

	if in.Page > 4 {
		get := config.Rdb.Get(config.Ctx, "token"+user.Mobile).Val()
		if get == "" {
			return nil, errors.New("用户未登录")
		}
	}

	var classLists []*__.ClassList
	for _, h := range classList {
		list := __.ClassList{
			Title:    h.Title,
			Info:     h.Info,
			CoverUrl: h.CoverUrl,
		}
		classLists = append(classLists, &list)
	}

	return &__.ClassListResp{
		List: classLists,
	}, nil
}

// 筛选
func (s *Server) ClassType(_ context.Context, in *__.ClassTypeReq) (*__.ClassTypeResp, error) {

	var class model.Class
	class.TypesId = uint(in.TypeId)

	classList, err := class.ListType()
	if err != nil {
		return nil, errors.New("数据查找失败")
	}

	var classLists []*__.ClassList
	for _, h := range classList {
		list := __.ClassList{
			Title:    h.Title,
			Info:     h.Info,
			CoverUrl: h.CoverUrl,
		}
		classLists = append(classLists, &list)
	}

	return &__.ClassTypeResp{
		List: classLists,
	}, nil
}

// 详情
func (s *Server) ClassDetail(_ context.Context, in *__.ClassDetailReq) (*__.ClassDetailResp, error) {
	var class model.Class
	class.Id = uint(in.Id)
	if err := class.Detail(); err != nil {
		return nil, errors.New("详情查询失败")
	}
	return &__.ClassDetailResp{
		Id:       int64(class.Id),
		Title:    class.Title,
		Info:     class.Info,
		CoverUrl: class.CoverUrl,
		VideoUrl: class.VideoUrl,
		VipPrice: float32(class.VipPrice),
		Price:    float32(class.Price),
		TypesId:  int64(class.TypesId),
	}, nil
}

// 详情
func (s *Server) LikeStatus(_ context.Context, in *__.LikeStatusReq) (*__.LikeStatusResp, error) {
	var class model.Class
	class.Id = uint(in.Id)
	if err := class.Detail(); err != nil {
		return nil, errors.New("详情查询失败")
	}
	if class.Status == 1 {
		return nil, errors.New("您未购买该课程,只能试看一章")
	}
	return &__.LikeStatusResp{
		Id:       int64(class.Id),
		Title:    class.Title,
		Info:     class.Info,
		CoverUrl: class.CoverUrl,
		VideoUrl: class.VideoUrl,
		VipPrice: float32(class.VipPrice),
		Price:    float32(class.Price),
		TypesId:  int64(class.TypesId),
	}, nil
}
